{template 'common/header'}
<script language="javascript" src="../addons/ewei_shopping/template/mobile/res/js/amrnb.js"></script>
<div class="panel panel-default">
    <div class="panel-heading">
       审核
    </div>
    <div class="panel-body">
       
        <input type="hidden" name="id" value="{$class['id']}" />
 
            <div class="form-group">
                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">语音内容</label>
                <div class="col-sm-8 col-lg-9 col-xs-12">
                    <p id="sample-amr">
                        <button>试听</button> <a href="http://0499.jiaoyinet.com/web/index.php?c=site&a=entry&do=classheader&m=ewei_shopping"></a>
                    </p>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <input name="submit" type="submit" value="审核" class="btn btn-primary col-lg-1">
                </div>
            </div>
       
    </div>
    
</div>
<script>
    function E(selector) {
        return document.querySelector(selector);
    }

    E('#sample-amr > button').onclick = function() {
        fetchBlob(E('#sample-amr > a').href, function(blob) {
            playAmrBlob(blob);
        });
    };


    var gAudioContext = new AudioContext();

    function getAudioContext() {
        if (!gAudioContext) {
            gAudioContext = new AudioContext();
        }
        return gAudioContext;
    }

    function fetchBlob(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.onload = function() {
            callback(this.response);
        };
        xhr.onerror = function() {
            alert('Failed to fetch ' + url);
        };
        xhr.send();
    }

    function readBlob(blob, callback) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var data = new Uint8Array(e.target.result);
            callback(data);
        };
        reader.readAsArrayBuffer(blob);
    }

    function fetchAndReadBlob(url, callback) {
        fetchBlob(url, function(blob) {
            readBlob(blob, callback);
        });
    }

    function playAmrBlob(blob, callback) {
        readBlob(blob, function(data) {
            playAmrArray(data);
        });
    }

    function convertAudioBlobToAmr(blob) {
        readBlob(blob, function(data) {
            var ctx = getAudioContext();
            ctx.decodeAudioData(data.buffer, function(audioBuffer) {
                var pcm;
                if (audioBuffer.copyFromChannel) {
                    pcm = new Float32Array(audioBuffer.length);
                    audioBuffer.copyFromChannel(pcm, 0, 0);
                } else {
                    pcm = audioBuffer.getChannelData(0);
                }
                var amr = AMR.encode(pcm, audioBuffer.sampleRate, 7);
                playAmrArray(amr);
            });
        });
    }

    function playAmrArray(array) {
        var samples = AMR.decode(array);
        if (!samples) {
            alert('Failed to decode!');
            return;
        }
        playPcm(samples);
    }

    function playPcm(samples) {
        var ctx = getAudioContext();
        var src = ctx.createBufferSource();
        var buffer = ctx.createBuffer(1, samples.length, 8000);
        if (buffer.copyToChannel) {
            buffer.copyToChannel(samples, 0, 0)
        } else {
            var channelBuffer = buffer.getChannelData(0);
            channelBuffer.set(samples);
        }

        src.buffer = buffer;
        src.connect(ctx.destination);
        src.start();
    }

    function convertAmrBlobToWav(blob) {
        readBlob(blob, function(data) {
            var buffer = AMR.toWAV(data);
            E('pre').textContent = toHex(buffer);
            var url = URL.createObjectURL(new Blob([buffer], { type: 'audio/x-wav' }));
            // Play wav buffer
            var audio = new Audio(url);
            audio.onloadedmetadata = audio.onerror = function() {
                URL.revokeObjectURL(url);
            };
            audio.play();
        });
    }

    function toHex(buffer) {
        var str = '';
        for (var i = 0; i < buffer.length; i++) {
            var s = buffer[i].toString(16);
            if (s.length == 1) {
                s = '0' + s;
            }
            str += s;
            if (i % 16 == 15) { // print 16 bytes per line
                str += '\n'
            } else if (i % 2 == 1) { // add a space seperator every two bytes.
                str += ' ';
            }
        }
        return str;
    }
</script>




{template 'common/footer'}